#include "mycoin.h"
#include <QDebug>
#include <QString>

//MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
//{

//}

MyCoin::MyCoin(QString btnImg){
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if(!ret){
        QString str = QString("图片%1").arg(btnImg);
        qDebug()<<str;
        return;
    }

    this->setFixedSize(pix.width(), pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(), pix.height()));

    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    connect(timer1, &QTimer::timeout,[=](){
       QPixmap pix;
       QString str = QString(":/res/Coin000%1.png").arg(this->min++);
       pix.load(str);
       this->setFixedSize(pix.width(), pix.height());
       this->setStyleSheet("QPushButton{border:0px;}");
       this->setIcon(pix);
       this->setIconSize(QSize(pix.width(), pix.height()));

       if(this->min > this->max){
           this->min = 1;
           isAnimation = false;
           timer1->stop();
       }
    });

    connect(timer2, &QTimer::timeout,[=](){
       QPixmap pix;
       QString str = QString(":/res/Coin000%1.png").arg(this->max--);
       pix.load(str);
       this->setFixedSize(pix.width(), pix.height());
       this->setStyleSheet("QPushButton{border:0px;}");
       this->setIcon(pix);
       this->setIconSize(QSize(pix.width(), pix.height()));

       if(this->max < this->min){
           this->max = 8;
           isAnimation = false;
           timer2->stop();
       }
    });
}

void MyCoin::changFlag(){
    if(this->flag){
        timer1->start(30);
        isAnimation = true;
        this->flag = false;
    }else {
        timer2->start(30);
        isAnimation = true;
        this->flag = true;
    }
}

int MyCoin::getX(){
    return this->posX;
}

int MyCoin::getY(){
    return this->posY;
}

void MyCoin::setX(int x){
    this->posX = x;
}

void MyCoin::setY(int y){
    this->posY = y;
}

void MyCoin::mousePressEvent(QMouseEvent *e){
    if(isAnimation == true || isWin == true){
        return;
    }
    QPushButton::mousePressEvent(e);
}


